package com.xydtech.ecard.custgroup.service.impl;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerGroupOrganize;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerOrganizeHandover;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerGroupOrganizeMapper;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerOrganizeHandoverMapper;
import com.xydtech.ecard.custgroup.service.IEcardApiCustomerGroupOrganizeService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社团组织信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-11-29 上午9:31
 */
@Service
public class EcardApiCustomerGroupOrganizeServiceImpl implements IEcardApiCustomerGroupOrganizeService {

    @Autowired
    private EcardApiCustomerGroupOrganizeMapper ecardCustomerGroupOrganizeMapper;
    @Autowired
    private EcardApiCustomerOrganizeHandoverMapper ecardCustomerOrganizeHandoverMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 条件查询社团组织信息
     * @param ecardApiCustomerGroupOrganize
     * @param salt
     * @return
     */
    @Override
    public List<EcardApiCustomerGroupOrganize> getOrganizeInfoList(EcardApiCustomerGroupOrganize ecardApiCustomerGroupOrganize, String salt) {
        List<EcardApiCustomerGroupOrganize> organizeInfoList = ecardCustomerGroupOrganizeMapper.getOrganizeInfoList(ecardApiCustomerGroupOrganize);
        if (organizeInfoList.size() > 0) {
            for (EcardApiCustomerGroupOrganize groupOrganize : organizeInfoList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(groupOrganize.getOrganizePhone())) {
                        groupOrganize.setOrganizePhone(AESEncryptionUtils.decrypt(groupOrganize.getOrganizePhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return organizeInfoList;
    }

    /**
     * 查询社团组织详情接口
     * @param id
     * @param salt
     * @return
     */
    @Override
    public EcardApiCustomerGroupOrganize selecteEardCustomerGroupOrganizeById(Long id, String salt) {
        EcardApiCustomerGroupOrganize customerGroupOrganize = ecardCustomerGroupOrganizeMapper.selecteEardCustomerGroupOrganizeById(id);
        if (customerGroupOrganize != null){
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(customerGroupOrganize.getOrganizePhone())) {
                    customerGroupOrganize.setOrganizePhone(AESEncryptionUtils.decrypt(customerGroupOrganize.getOrganizePhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return customerGroupOrganize;
    }

    /**
     * 唯一值验证，相同法人机构、社团组织名称不能重复
     * @param ecardCustomerGroupOrganize
     * @return
     */
    @Override
    public int validateGroupOrganizeInfo(EcardApiCustomerGroupOrganize ecardCustomerGroupOrganize) {
        return ecardCustomerGroupOrganizeMapper.validateGroupOrganizeInfo(ecardCustomerGroupOrganize);
    }

    /**
     * 保存社团组织信息
     * @param ecardCustomerGroupOrganize
     * @param salt
     * @return
     */
    @Override
    public int addCustomerGroupOrganize(EcardApiCustomerGroupOrganize ecardCustomerGroupOrganize, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardCustomerGroupOrganize.getOrganizePhone())) {
                ecardCustomerGroupOrganize.setOrganizePhone(AESEncryptionUtils.encrypt(ecardCustomerGroupOrganize.getOrganizePhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardCustomerGroupOrganizeMapper.addCustomerGroupOrganize(ecardCustomerGroupOrganize);
    }

    /**
     * 社团组织转交
     * @param ecardCustomerOrganizeHandover
     * @return
     */
    @Override
    public int addOrganizeTransferInfo(EcardApiCustomerOrganizeHandover ecardCustomerOrganizeHandover) {
        //保存转交记录
        ecardCustomerOrganizeHandoverMapper.insertEcardCustomerOrganizeHandover(ecardCustomerOrganizeHandover);
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ecardCustomerOrganizeHandover.getRecipientDeptId());
        //更新社团组织信息的管户人id和机构id
        return ecardCustomerGroupOrganizeMapper.updateOrganizeUserId(ecardCustomerOrganizeHandover.getOrganizeId(), ecardCustomerOrganizeHandover.getRecipientId(), ecardCustomerOrganizeHandover.getRecipientDeptId(), sysDept.getDeptId());
    }

    /**
     * 根据ID修改社团组织信息状态
     * @param id
     * @return
     */
    @Override
    public int changeOrganizeStatus(Long id) {
        return ecardCustomerGroupOrganizeMapper.changeOrganizeStatus(id);
    }
}
